package chapter4.namedJDBC;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import chapter4.namedJDBC.User;


public class UserDao {
	private static String tablename = "student";
	private NamedParameterJdbcTemplate npjt;
	
	public NamedParameterJdbcTemplate getNpjt() {
		return npjt;
	}

	public void setNpjt(NamedParameterJdbcTemplate npjt) {
		this.npjt = npjt;
	}
	/*NamedParameterJdbcTemplate使用：Map传参*/
	public void add(Map paramMap){
		String sql=String.format("insert into %s (id,name,age) values (:id,:name,:age)", tablename);
		npjt.update(sql, paramMap);		
	}
	/*NamedParameterJdbcTemplate使用：实体类（User）传参*/
	public void addUser(User user){
		String sql = String.format("insert into %s (id,name,age) values (:id,:name,:age)", tablename);
		SqlParameterSource paramSource=new BeanPropertySqlParameterSource(user);
		npjt.update(sql, paramSource);
	}
	/*queryForList()方法*/
	public List<Map<String,Object>> findAllMap(Map paramMap){
		String sql = String.format("SELECT * FROM %s",tablename);
		return npjt.queryForList(sql, paramMap);
	}	
}
